package com.song.controller.admin;

import com.song.dto.DishDTO;
import com.song.dto.DishPageQueryDTO;
import com.song.entity.Dish;
import com.song.result.PageResult;
import com.song.result.Result;
import com.song.service.DishService;
import com.song.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@RestController("admin-DishController")
@RequestMapping("/admin/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    //新增菜品接口
    @CacheEvict(cacheNames = "dishCache",key = "#dishDTO.id")
    @PostMapping
    public Result addDish(@RequestBody DishDTO dishDTO){
        log.info("新增菜品接口,菜品参数：{}",dishDTO);
       return dishService.addDish(dishDTO) == 1 ? Result.success(null,"添加菜品成功") : Result.error("添加失败");
    }

    //分页查询菜品接口
    @Cacheable(cacheNames = "dishCache",key = "#dishPageQueryDTO.categoryId")
    @GetMapping("/page")
    public Result<PageResult<DishVO>> getDishPage(DishPageQueryDTO dishPageQueryDTO){
        log.info("分页查询菜品接口,参数：{}",dishPageQueryDTO);
        PageResult<DishVO> pageResult = dishService.getDishPage(dishPageQueryDTO);
        return Result.success(pageResult,"菜品分页查询成功");
    }

    //批量删除菜品接口
    @CacheEvict(cacheNames = "dishCache",allEntries = true)
    @DeleteMapping
    public Result deleteDish(@RequestParam List<Long> ids){
        log.info("批量删除菜品接口,参数：{}",ids);
        dishService.deleteDish(ids);
      return Result.success(null,"菜品数据删除成功");
    }
    //修改菜品接口
    @CacheEvict(cacheNames = "dishCache",key = "#dishDTO.id")
    @PutMapping
    public Result updateDish(@RequestBody DishDTO dishDTO){
        log.info("修改菜品接口,参数：{}",dishDTO);
        return dishService.updateDish(dishDTO) == 1 ? Result.success(null,"修改成功") : Result.error("修改失败");
    }

    //根据菜品id查询菜品接口
    @Cacheable(cacheNames = "dishCache",key = "#id")
    @GetMapping("/{id}")
    public Result<DishVO> getDishById(@PathVariable Long id){
        log.info("根据菜品id查询菜品接口,参数：{}",id);
       return Result.success(dishService.getDishByID(id),"查询成功");
    }
    //根据菜品分类id查询菜品列表数据接口
    @Cacheable(cacheNames = "dishCache",key = "#categoryId")
    @GetMapping("/list")
    public Result<List<Dish>> getDishListByCategoryId(Long categoryId){
        log.info("根据菜品分类id查询菜品列表数据接口,参数分类id：{}",categoryId);
        return Result.success(dishService.getDishListByCategoryId(categoryId),"查询成功");
    }
    //根据菜品id起售停售接口
    @CacheEvict(cacheNames = "dishCache",key="#id")
    @PostMapping("/status/{status}")
    public Result startOrStop(@PathVariable Integer status,Long id){
        log.info("菜品起售停售接口,参数菜品id：{},要设置的菜品状态：{}",id,status);
        return dishService.updateDishStatus(status,id) == 1 ? Result.success(null,"操作成功") : Result.error("操作失败");
    }


}

